package com.atguigu.edu.publisher.controller;

import com.atguigu.edu.publisher.beans.*;
import com.atguigu.edu.publisher.service.TradeStatsService;
import com.atguigu.edu.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TradeController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/provinceUserCt")
    public String getProvinceUserCt(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceUserCt> tradeProvinceUserCtList = tradeStatsService.getTradeProvinceUserCt(date);
        if (tradeProvinceUserCtList == null) {
            return "";
        }
        StringBuilder mapData = new StringBuilder("[");
        for (int i = 0; i < tradeProvinceUserCtList.size(); i++) {
            TradeProvinceUserCt tradeProvinceUserCt = tradeProvinceUserCtList.get(i);
            String provinceName = tradeProvinceUserCt.getProvinceName();
            Integer UserCt = tradeProvinceUserCt.getUserCt();
            mapData.append("{\n" +
                    "        \"name\": \"" + provinceName + "\",\n" +
                    "        \"value\": " + UserCt + "\n" +
                    "      }");
            if (i < tradeProvinceUserCtList.size() - 1) {
                mapData.append(",");
            } else {
                mapData.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + mapData + ",\n" +
                "    \"valueName\": \"下单人数\"\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/provinceOrderCt")
    public String getProvinceOrderCt(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderCt> tradeProvinceOrderCtList = tradeStatsService.getTradeProvinceOrderCt(date);
        if (tradeProvinceOrderCtList == null) {
            return "";
        }
        StringBuilder mapData = new StringBuilder("[");
        for (int i = 0; i < tradeProvinceOrderCtList.size(); i++) {
            TradeProvinceOrderCt tradeProvinceOrderCt = tradeProvinceOrderCtList.get(i);
            String provinceName = tradeProvinceOrderCt.getProvinceName();
            Integer orderCt = tradeProvinceOrderCt.getOrderCt();
            mapData.append("{\n" +
                    "        \"name\": \"" + provinceName + "\",\n" +
                    "        \"value\": " + orderCt + "\n" +
                    "      }");
            if (i < tradeProvinceOrderCtList.size() - 1) {
                mapData.append(",");
            } else {
                mapData.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + mapData + ",\n" +
                "    \"valueName\": \"下单次数\"\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/provinceOrderAmount")
    public String getProvinceOrderAmount(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> tradeProvinceOrderAmountList = tradeStatsService.getTradeProvinceOrderAmount(date);
        if (tradeProvinceOrderAmountList == null) {
            return "";
        }
        StringBuilder mapData = new StringBuilder("[");
        for (int i = 0; i < tradeProvinceOrderAmountList.size(); i++) {
            TradeProvinceOrderAmount tradeProvinceOrderAmount = tradeProvinceOrderAmountList.get(i);
            String provinceName = tradeProvinceOrderAmount.getProvinceName();
            Double orderAmount = tradeProvinceOrderAmount.getOrderAmount();
            mapData.append("{\n" +
                    "        \"name\": \"" + provinceName + "\",\n" +
                    "        \"value\": " + orderAmount + "\n" +
                    "      }");
            if (i < tradeProvinceOrderAmountList.size() - 1) {
                mapData.append(",");
            } else {
                mapData.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + mapData + ",\n" +
                "    \"valueName\": \"订单金额\"\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/sourceOrderAmount")
    public String getSourceOrderAmount(@RequestParam(value="date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeSourceOrderAmount> tradeSourceOrderAmountList = tradeStatsService.getTradeSourceOrderAmount(date);
        StringBuilder categories = new StringBuilder("[");
        StringBuilder orderAmountValues = new StringBuilder("[");

        for (int i = 0; i < tradeSourceOrderAmountList.size(); i++) {
            TradeSourceOrderAmount tradeSourceOrderAmount = tradeSourceOrderAmountList.get(i);
            String sourceSite = tradeSourceOrderAmount.getSourceSite();
            Double orderAmount = tradeSourceOrderAmount.getOrderAmount();

            categories.append("\"").append(sourceSite).append("\"");
            orderAmountValues.append("\"").append(orderAmount).append("\"");

            if (i < tradeSourceOrderAmountList.size() - 1) {
                categories.append(",");
                orderAmountValues.append(",");
            } else {
                categories.append("]");
                orderAmountValues.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":" + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"销售额\",\n" +
                "        \"data\": " + orderAmountValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/sourceOrderCt")
    public String getSourceOrderCt(@RequestParam(value="date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeSourceOrderCt> tradeSourceOrderCtList = tradeStatsService.getTradeSourceOrderCt(date);
        StringBuilder categories = new StringBuilder("[");
        StringBuilder orderCtValues = new StringBuilder("[");

        for (int i = 0; i < tradeSourceOrderCtList.size(); i++) {
            TradeSourceOrderCt tradeSourceOrderCt = tradeSourceOrderCtList.get(i);
            String sourceSite = tradeSourceOrderCt.getSourceSite();
            Integer orderCt = tradeSourceOrderCt.getOrderCt();

            categories.append("\"").append(sourceSite).append("\"");
            orderCtValues.append("\"").append(orderCt).append("\"");

            if (i < tradeSourceOrderCtList.size() - 1) {
                categories.append(",");
                orderCtValues.append(",");
            } else {
                categories.append("]");
                orderCtValues.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":" + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"销售额\",\n" +
                "        \"data\": " + orderCtValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/sourceUserCt")
    public String getSourceUserCt(@RequestParam(value="date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeSourceUserCt> tradeSourceUserCtList = tradeStatsService.getTradeSourceUserCt(date);
        StringBuilder categories = new StringBuilder("[");
        StringBuilder userCtValues = new StringBuilder("[");

        for (int i = 0; i < tradeSourceUserCtList.size(); i++) {
            TradeSourceUserCt tradeSourceUserCt = tradeSourceUserCtList.get(i);
            String sourceSite = tradeSourceUserCt.getSourceSite();
            Double userCt = tradeSourceUserCt.getUserCt();

            categories.append("\"").append(sourceSite).append("\"");
            userCtValues.append("\"").append(userCt).append("\"");

            if (i < tradeSourceUserCtList.size() - 1) {
                categories.append(",");
                userCtValues.append(",");
            } else {
                categories.append("]");
                userCtValues.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":" + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"销售额\",\n" +
                "        \"data\": " + userCtValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/sourceInversionRate")
    public String getSourceInversionRate(@RequestParam(value="date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeSourceInversionRate> tradeSourceInversionRateList = tradeStatsService.getTradeSourceInversionRate(date);
        StringBuilder categories = new StringBuilder("[");
        StringBuilder rateValues = new StringBuilder("[");

        for (int i = 0; i < tradeSourceInversionRateList.size(); i++) {
            TradeSourceInversionRate tradeSourceInversionRate = tradeSourceInversionRateList.get(i);
            String sourceSite = tradeSourceInversionRate.getSourceSite();
            Double rate = tradeSourceInversionRate.getInversionRate();

            categories.append("\"").append(sourceSite).append("\"");
            rateValues.append("\"").append(rate).append("\"");

            if (i < tradeSourceInversionRateList.size() - 1) {
                categories.append(",");
                rateValues.append(",");
            } else {
                categories.append("]");
                rateValues.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":" + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"销售额\",\n" +
                "        \"data\": " + rateValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }
}
